<template>
  <scale-box :wrapper-color="'#1C1C1C'" @scale-change="scaleChange">
    <div class="container">
      <el-button class="logout" size="mini" @click="logout">退 出</el-button>
      <CustomTitle />
      <OverallState class="overall-state" :overall="overall" />
      <ResCard class="res-card" :res-card="resCard" />
      <Top5Usage class="top5-usage" />
      <WarningAndAlarm class="warning-adn-alarm" />
      <CenterStat
        ref="centerStat"
        class="center-stat"
        :provider-topo="providerTopo"
      />
      <!-- <Top5Cluster class="top5-cluster" /> -->
      <div class="top5-cluster" @click="jumpTo('monitor/cluster')">
        <!-- 标题 -->
        <div class="title">集群资源监控Top5(CPU)</div>
        <!-- 集群监控跑马灯-->
        <el-carousel
          ref="appCarousel"
          height="200px"
          class="app-status-ratio"
          arrow="never"
          :interval="appStatusTime"
          trigger="click"
        >
          <!-- 集群1
                数据传输-->
          <el-carousel-item v-if="clusterMonitor">
            <Top5Cluster :app-status-ratio="clusterMonitor" />
            <!-- 集群2
                数据传输-->
          </el-carousel-item>
          <el-carousel-item v-if="clusterMonitor2">
            <Top5Cluster :app-status-ratio="clusterMonitor2" />
            <!-- 集群3
                数据传输-->
          </el-carousel-item>
          <el-carousel-item v-if="clusterMonitor3">
            <Top5Cluster :app-status-ratio="clusterMonitor3" />
            <!-- 集群4
                数据传输-->
          </el-carousel-item>
          <el-carousel-item v-if="clusterMonitor4">
            <Top5Cluster :app-status-ratio="clusterMonitor4" />
            <!-- 集群5
                数据传输-->
          </el-carousel-item>
          <el-carousel-item v-if="clusterMonitor5">
            <Top5Cluster :app-status-ratio="clusterMonitor5" />
          </el-carousel-item>
        </el-carousel>
      </div>
      <WorkloadMonitor class="workload-monitor" />
      <ContainerMonitor class="container-monitor" />
      <Top5Network class="top5-network" />
    </div>
  </scale-box>
</template>

<script>
import ScaleBox from '@/components/ScaleBox/index'
import { logout } from '@/api/user'
import { getMonitorOverview } from '@/api/situation'
import { getClusterTop5 } from '@/api/cnap/situation'
import CustomTitle from './components/CustomTitle.vue'
import OverallState from './components/OverallState.vue'
import ResCard from './components/ResCard.vue'
import Top5Usage from './components/Top5Usage.vue'
import WarningAndAlarm from './components/WarningAndAlarm.vue'
import CenterStat from './components/CenterStat.vue'
import Top5Cluster from './components/Top5Cluster.vue'
import WorkloadMonitor from './components/WorkloadMonitor.vue'
import ContainerMonitor from './components/ContainerMonitor.vue'
import Top5Network from './components/Top5Network.vue'

export default {
  name: 'Situation',
  components: {
    ScaleBox,
    CustomTitle,
    OverallState,
    ResCard,
    Top5Usage,
    WarningAndAlarm,
    CenterStat,
    Top5Cluster,
    Top5Network,
    ContainerMonitor,
    WorkloadMonitor
  },
  data() {
    return {
      overall: [],
      resCard: [],
      providerTopo: [],
      clusterMonitor: null, // 集群1数据
      clusterMonitor2: null, // 集群2数据
      clusterMonitor3: null, // 集群3数据
      clusterMonitor4: null, // 集群4数据
      clusterMonitor5: null, // 集群5数据
      appStatusTime: 5000 // 跑马灯自动轮转时间
    }
  },
  computed: {},
  created() {
    this.initData()
    this.getCluster()
  },
  methods: {
    /**
     * @description: 登出
     * @return {*}
     */
    logout() {
      // 弹窗
      this.$confirm('确定退出登录吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        type: 'warning'
      }).then(() => {
        logout() // 登出
        setTimeout(() => {
          // store 重置状态
          this.$store.dispatch('user/resetState').then(() => {
            // 成功后去登录页面
            this.$router.replace({
              path: '/login'
            })
          })
        }, 300)
      })
    },
    /**
     * @description: 缩放容器变化
     * @param {*} val
     * @return {*}
     */
    scaleChange(val) {
      this.$refs.centerStat.scaleChangeHandle(val)
    },
    /**
     * @description: 获取数据
     * @return {*}
     */
    getPageData() {
      getMonitorOverview().then((res) => {
        this.overall =
          res.data.overview && Array.isArray(res.data.overview.items)
            ? res.data.overview.items
            : []
        this.resCard = Array.isArray(res.data.resourceViews)
          ? res.data.resourceViews
          : []
        const providerTopo = Array.isArray(res.data.providerViews)
          ? res.data.providerViews
          : []
        this.providerTopo = providerTopo.slice(
          0,
          Math.min(providerTopo.length, 4)
        )
      })
    },
    /**
     * @description: 初始化数据
     * @return {*}
     */
    initData() {
      this.getPageData()
    },
    /**
     * @description: 获取数据
     * @description: 集群监控Top5
     * @return {*}
     */
    getCluster() {
      getClusterTop5().then(res => {
        // code==200是正常返回
        if (res.code === 200) {
          // 返回结果取值
          // 信息在res.data.data中
          // let res = {"code":200,"message":"OK","data":[{"cluster":"tencentCloudv4c109d1d","clusterProvider":"tencentCloud","id":"tencentCloud:tencentCloudv4c109d1d","data":[{"id":"CPU","name":"CPU","value":"0.04"},{"id":"pod","name":"容器组","value":"0.12"},{"id":"memory","name":"内存","value":"0.43"},{"id":"disk","name":"磁盘","value":"0.12"}]}]}
          for (let i = 0; i < res.data.length; i++) {
            for (let j = 0; j < res.data[i].data.length; j++) {
              res.data[i].data[j].id = res.data[i].data[j].id + i + j// 每个echarts都必须有个不同的ID
            }
          }
          this.clusterMonitor = res.data[0]// 集群1组件数据
          this.clusterMonitor2 = res.data[1]// 集群2组件数据
          this.clusterMonitor3 = res.data[2]// 集群3组件数据
          this.clusterMonitor4 = res.data[3]// 集群4组件数据
          this.clusterMonitor5 = res.data[4]// 集群5组件数据
        } else {
          (
            // 数据查询失败
            // 输出报错信息
            this.$message.error(res.message)
          )
        }
      })
    },
    /**
     * @description: 跳转
     * @description: 跳转集群监控页面
     * @return {*}
     */
    jumpTo(path) {
      this.$router.replace({
        path: path
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.container {
  width: 1920px;
  height: 1080px;
  background: url("~@/assets/imgs/situation/bg_index.png") no-repeat;
  background-size: 100% 100%;
  position: relative;
  .title{
    width: 494px;
    height: 28px;
    line-height: 28px;
    background: url("~@/assets/imgs/situation/bg_title_normal.png") no-repeat;
    background-size: 100% 100%;
    box-sizing: border-box;
    padding-left: 25px;
    .textStyle(16px , 600, #03d3ff);
  }
  .logout {
    position: absolute;
    top: 18px;
    right: 28px;
    padding: 0;
    width: 48px;
    height: 22px;
    line-height: 20px;
    background: linear-gradient(
      270deg,
      rgba(44, 205, 251, 0.3) 0%,
      rgba(38, 175, 255, 0.3) 100%
    );
    box-shadow: 0px 0px 6px 0px rgba(0, 97, 154, 0.5);
    border-radius: 2px;
    border: 1px solid #00a1ff;
    .textStyle(12px, 500, #52bfff);
  }

  .overall-state {
    position: absolute;
    top: 98px;
    left: 50%;
    transform: translateX(-50%);
  }

  .res-card {
    position: absolute;
    top: 90px;
    left: 30px;
  }

  .top5-usage {
    position: absolute;
    bottom: 38px;
    left: 30px;
  }

  .warning-adn-alarm {
    position: absolute;
    bottom: 38px;
    left: 50%;
    transform: translateX(-50%);
  }

  .center-stat {
    position: absolute;
    top: 248px;
    left: 50%;
    transform: translateX(-50%);
  }

  .top5-cluster {
    cursor: pointer;
    position: absolute;
    top: 90px;
    right: 30px;
  }

  .workload-monitor {
    cursor: pointer;
    position: absolute;
    top: 361px;
    right: 30px;
  }

  .container-monitor {
    cursor: pointer;
    position: absolute;
    top: 624px;
    right: 30px;
  }

  .top5-network {
    position: absolute;
    bottom: 38px;
    right: 30px;
  }
}
</style>
<style lang="less">
.container {
  .app-status-ratio {
    margin-top: 8px;
    box-sizing: border-box;
    padding: 8px 10px 0;
    height: 220px;
    background: linear-gradient(
      351deg,
      rgba(60, 108, 158, 0.37) 0%,
      rgba(19, 61, 91, 0.35) 100%
    );
    .el-carousel__button {
      width: 5px !important;
      height: 5px !important;
    }
    .el-carousel__indicator--horizontal {
      padding: 0px 4px !important;
    }
    .el-carousel__indicators--horizontal {
    bottom: 0px;
    }
  }
}
</style>
